package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.spa.infrastructure.repository.condition.ShopUserCondition;
import com.spa.infrastructure.repository.mapper.ShopUserMapper;
import com.spa.infrastructure.repository.po.ShopPO;
import com.spa.infrastructure.repository.po.ShopUserPO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class ShopUserRepositoryService extends ServiceImpl<ShopUserMapper, ShopUserPO> {

    @Resource
    private ShopUserMapper shopUserMapper;

    public ShopUserPO getShopUserByCondition(ShopUserCondition condition) {
        LambdaQueryWrapper<ShopUserPO> lqw = wrapper(condition);
        return shopUserMapper.selectOne(lqw);
    }

    public ShopUserPO getShopUserPOById(long id) {
        return getById(id);
    }

    public void addShopUser(ShopUserPO shopUserPO) {
        shopUserMapper.insert(shopUserPO);
    }

    public void delShopUserById(Long id) {
        shopUserMapper.deleteById(id);
    }

    public Page<ShopUserPO> page(ShopUserCondition condition) {
        LambdaQueryWrapper<ShopUserPO> wrapper = wrapper(condition);
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper);
    }

    public List<ShopUserPO> list(ShopUserCondition condition) {
        LambdaQueryWrapper<ShopUserPO> wrapper = wrapper(condition);
        return list(wrapper);
    }

    private LambdaQueryWrapper<ShopUserPO> wrapper(ShopUserCondition condition) {
        LambdaQueryWrapper<ShopUserPO> wrapper = Wrappers.lambdaQuery();
        wrapper.in(CollectionUtil.isNotEmpty(condition.getIds()), ShopUserPO::getId, condition.getIds());
        wrapper.eq(ObjectUtil.isNotNull(condition.getId()), ShopUserPO::getId, condition.getId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), ShopUserPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getPhone()), ShopUserPO::getPhone, condition.getPhone());
        wrapper.eq(ObjectUtil.isNotNull(condition.getIsPrincipal()), ShopUserPO::getIsPrincipal, condition.getIsPrincipal());
        wrapper.orderByDesc(ShopUserPO::getCreateTime);
        return wrapper;
    }

    public List<ShopUserPO> listJoinShop(ShopUserCondition condition) {
        return list(joinShopWrapper(condition));
    }

    private MPJLambdaWrapper<ShopUserPO> joinShopWrapper(ShopUserCondition condition) {
        MPJLambdaWrapper<ShopUserPO> mpjWrapper = JoinWrappers.lambda(ShopUserPO.class);
        mpjWrapper.leftJoin(ShopPO.class, ShopPO::getId, ShopUserPO::getShopId);
        mpjWrapper.selectAll(ShopUserPO.class);
        mpjWrapper.selectAs(ShopPO::getName, "shopName");
        mpjWrapper.in(CollectionUtil.isNotEmpty(condition.getIds()), ShopUserPO::getId, condition.getIds());
        mpjWrapper.eq(ObjectUtil.isNotNull(condition.getId()), ShopUserPO::getId, condition.getId());
        mpjWrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), ShopUserPO::getShopId, condition.getShopId());
        mpjWrapper.eq(ObjectUtil.isNotNull(condition.getPhone()), ShopUserPO::getPhone, condition.getPhone());
        mpjWrapper.eq(ObjectUtil.isNotNull(condition.getIsPrincipal()), ShopUserPO::getIsPrincipal, condition.getIsPrincipal());
        mpjWrapper.orderByDesc(ShopUserPO::getCreateTime);
        return mpjWrapper;
    }

}
